/**
 * @author Juntalis
 */
var App = {
	// Settings Manager
	
	SettingsManager : new SettingsManager(),
	// Called when the settings are loaded.
	onSettingsLoaded : function() {
		if (Settings.User.AutoLogin == "true") {
			Bot.Name = Settings.User.Name;
			Bot.Password = md5(Settings.User.Password);
			UI.Manager.LoadInterface(Interfaces("Scripting"));
		} else {
			UI.Manager.LoadInterface(Interfaces("UserLogin"));
		}
	},
	Exit : function(){
		var exitingEvent = new air.Event(air.Event.EXITING, false, true);
		air.NativeApplication.nativeApplication.dispatchEvent(exitingEvent);
		if (!exitingEvent.isDefaultPrevented()) {
			air.NativeApplication.nativeApplication.exit();
		}
	},
	
	Version : "2.0 Beta"
}

// Entry point of our program.
// Loads the AppSettings
$(document).ready(function(){
	
	// Load Application Updater
	var AppUpdater = new runtime.air.update.ApplicationUpdaterUI(); 
	AppUpdater.configurationFile = new air.File("app:/config/updater.xml");
	AppUpdater.initialize();
	
	// PreLoad Images.
	$.preloadImages("img/ani-loading.gif","img/darkbar.png","img/darkbar-c.png","img/darkbar-d.png","img/darkbar-h.png");
	
	// Bind the link up top to open in a new window.
	$("#TBHAuthorWeb").click(function() {
		Misc.OpenUrl("http://juntalis.com");
	});
	
	// Load User Bot
	if (typeof(_UserBotInit) != "undefined") {
		try {
			_UserBotInit();
		} catch(error) {
			alert("Error while loading bot script file.\n\n" + error);
			_resetBotScript();
		}
	}
	// Check for settings and load them.
	var dbSettings = air.File.applicationStorageDirectory.resolvePath("AppSettings.db");
	if(!dbSettings.exists )
		App.SettingsManager.CreateSettings(dbSettings);
	App.SettingsManager.LoadSettings(dbSettings);
});